package ext.tianma.masschange.builder;

import java.util.Vector;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import wt.fc.QueryResult;
import wt.part.WTPart;
import wt.part.WTPartHelper;
import wt.part.WTPartMaster;
import wt.util.WTException;

import com.ptc.jca.mvc.components.JcaComponentParams;
import com.ptc.mvc.components.AbstractComponentBuilder;
import com.ptc.mvc.components.ColumnConfig;
import com.ptc.mvc.components.ComponentBuilder;
import com.ptc.mvc.components.ComponentConfig;
import com.ptc.mvc.components.ComponentConfigFactory;
import com.ptc.mvc.components.ComponentParams;
import com.ptc.mvc.components.TableConfig;
import com.ptc.mvc.util.ClientMessageSource;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.netmarkets.util.beans.NmHelperBean;

import ext.com.core.CoreUtil;
import ext.tianma.masschange.util.MassChangeUtil;

@ComponentBuilder(value = "ext.tianma.masschange.builder.NoYeildMassChangeSearchResultTableBuilder")
public class NoYeildMassChangeSearchResultTableBuilder extends AbstractComponentBuilder {
	
	private final ClientMessageSource messageSource = getMessageSource("ext.tianma.masschange.resource.MassChangeResource");
	
	
	public NoYeildMassChangeSearchResultTableBuilder() {
		
	}
	
	/**
	 * 获取数据
	 */
	public Object buildComponentData(ComponentConfig componentconfig, ComponentParams componentparams)
	throws Exception {
		
		Vector objects = new Vector();
		Vector parentParts = new Vector();
		NmHelperBean nmhelperbean = ((JcaComponentParams) componentparams).getHelperBean();
		NmCommandBean nmcommandbean = nmhelperbean.getNmCommandBean();
			
		//获取cookie中的值
		HttpServletRequest request = nmcommandbean.getRequest();
		Cookie[] cookies = request.getCookies();
		String SUBSTITUTE_MASSCHANGE_TYPE = "";
		String CHILD_PART_NUMBER = "";
		String CHILD_PART_VIEW = "";
		//获取cookie中的参照零件的编码和视图以及添加的part
		for (int i = 0; i < cookies.length; i++) {
			Cookie c = cookies[i];
			if(c.getName().equalsIgnoreCase("SUBSTITUTE_MASSCHANGE_TYPE")) {
				SUBSTITUTE_MASSCHANGE_TYPE = c.getValue().trim();
			} else if(c.getName().equalsIgnoreCase("MAIN_MATERIAL_NUMBER_VALUE")) {
				CHILD_PART_NUMBER = c.getValue().trim();
			} else if(c.getName().equalsIgnoreCase("MAIN_MATERIAL_VIEW_VALUE")) {
				CHILD_PART_VIEW = c.getValue().trim();
			}
		}
		//获取父子件link列表
		Vector links = new Vector();
		WTPart childPart = null;
		if(CHILD_PART_NUMBER!="" && CHILD_PART_VIEW!="") {
			childPart = CoreUtil.getWTPartByNumberAndView(CHILD_PART_NUMBER, CHILD_PART_VIEW);
		}
		
		if(childPart!=null) {							
			QueryResult qr = WTPartHelper.service.getUsedByWTParts((WTPartMaster) childPart.getMaster());				
			while(qr.hasMoreElements()) {
				WTPart parentPart = (WTPart)qr.nextElement();
				if(parentParts!=null && parentParts.contains(parentPart)) 
					continue;
				if(MassChangeUtil.isLatestVersion(parentPart)) {
					parentParts.add(parentPart);					
					Vector link = MassChangeUtil.getPartUsageLink(parentPart, childPart);
					links.addAll(link);
				}
			}						
		}
		objects.addAll(links);
		return objects;	
	}
	
	/**
	 * 构造元件显示配置
	 */
	public ComponentConfig buildComponentConfig(ComponentParams componentparams)
	throws WTException {
		ComponentConfigFactory componentconfigfactory = getComponentConfigFactory();
		TableConfig tableconfig = componentconfigfactory.newTableConfig();
		tableconfig.setLabel(messageSource.getMessage("PARENT_LIST"));
		tableconfig.setSelectable(true);
		
		ColumnConfig  numberConfig = componentconfigfactory.newColumnConfig("pNumber", true);
		numberConfig.setLabel(messageSource.getMessage("PARENT_NUMBER"));
		numberConfig.setDataUtilityId("MassChangeNumberDataUtility");
		tableconfig.addComponent(numberConfig);

		ColumnConfig  nameConfig = componentconfigfactory.newColumnConfig("pName", true);
		nameConfig.setLabel(messageSource.getMessage("CUST_NANE"));
		nameConfig.setDataUtilityId("MassChangeNameDataUtility");
		tableconfig.addComponent(nameConfig);
		
		ColumnConfig  viewConfig = componentconfigfactory.newColumnConfig("pView", true);
		viewConfig.setLabel(messageSource.getMessage("CUST_VIEW"));
		viewConfig.setDataUtilityId("MassChangeViewDataUtility");
		tableconfig.addComponent(viewConfig);
		
		ColumnConfig  stateConfig = componentconfigfactory.newColumnConfig("pState", true);
		stateConfig.setLabel(messageSource.getMessage("CUST_STATE"));
		stateConfig.setDataUtilityId("MassChangeStateDataUtility");
		tableconfig.addComponent(stateConfig);
		
		ColumnConfig  quantityConfig = componentconfigfactory.newColumnConfig("pQuantity", true);
		quantityConfig.setLabel(messageSource.getMessage("SUBSTITUTE_QUANTITY"));
		quantityConfig.setDataUtilityId("MassChangeQuantityDataUtility");
		tableconfig.addComponent(quantityConfig);
		
		ColumnConfig  unitConfig = componentconfigfactory.newColumnConfig("pUnit", true);
		unitConfig.setLabel(messageSource.getMessage("SUBSTITUTE_UNIT"));
		unitConfig.setDataUtilityId("MassChangeUnitDataUtility");
		tableconfig.addComponent(unitConfig);
		
		ColumnConfig  checkListConfig = componentconfigfactory.newColumnConfig("pCheckList", true);
		checkListConfig.setLabel(messageSource.getMessage("CHECK_LIST"));
		checkListConfig.setDataUtilityId("MassChangeCheckListDataUtility");
		tableconfig.addComponent(checkListConfig);
		
		ColumnConfig nonSelect = componentconfigfactory.newColumnConfig("pNonSelect",  false);
		nonSelect.setDataStoreOnly(true);
		nonSelect.setDataUtilityId("MassChangeNonSelectableRowDataUtility");
		tableconfig.addComponent(nonSelect);
		tableconfig.setNonSelectableColumn(nonSelect);
				
		return tableconfig;
	}
}

